<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" style="padding:10px 40px" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="上级部门" prop="parentName">
        <!-- <el-input v-model="dataForm.parentName" maxlength="6" show-word-limit placeholder="上级部门名称"></el-input> -->
        <el-select style="width: 100%;" v-model="value" placeholder="请选择" @focus="dataEditListFilterById(dataForm.id)" filterable>
        <el-option
          v-for="item in dataEditList"
          :key="item.value"
          :label="item.deptName"
          :value="item.id">
        </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="新增部门" prop="deptName">
        <el-input v-model="dataForm.deptName" maxlength="6" show-word-limit placeholder="下级部门名称"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" maxlength="6" show-word-limit controls-position="right" :min="0" label="排序号"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          tenantId: '',
          id: 0,
          deptName: '',
          parentId: '',
          parentName: '',
          sort: ''

        },
        value: '',
        statuses: [
          {label: '无效', value: 0},
          {label: '有效', value: 1}
        ],
        dataRule: {
/*          parentName: [
            {
              required: true, message: '部门名称不能为空', trigger: 'blur'

            }
          ], */
          deptName: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    props: {
      dataEditList: {
        type: Array
      }
    },
    mounted () {
      this.restForm()
    },
    created () {

    },
    methods: {
      dataEditListFilterById (id) {
        if (id !== 0) {
          this.dataEditList = this.dataEditList.filter(item => item.id !== id)
        }
      },
      restForm () {
        this.dataForm = {
          tenantId: '',
          id: 0,
          deptName: '',
          parentId: '',
          parentName: '',
          sort: '',
          post: ''
        }
      },
      init (tenantId, parentId, parentName, id) {
        this.visible = true
        this.restForm()
        this.dataForm.tenantId = tenantId || 0
        this.value = parentId || 0
        this.dataForm.parentName = parentName
        this.dataForm.id = id || 0
        if (this.dataForm.id > 0) {
          this.$http({
            url: this.$http.adornUrl(`/tenant/tenantDeptInfo/info/${this.dataForm.id}`),
            method: 'get',
            params: {}
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.tenantId = data.tenantDeptInfo.tenantId
              this.dataForm.deptName = data.tenantDeptInfo.deptName
              this.dataForm.parentId = data.tenantDeptInfo.parentId || ''
              this.dataForm.sort = data.tenantDeptInfo.sort
              this.dataForm.status = data.tenantDeptInfo.status
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tenant/tenantDeptInfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id,
                'tenantId': this.dataForm.tenantId,
                'deptName': this.dataForm.deptName,
                'parentId': this.value,
                'sort': this.dataForm.sort,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }

    }
  }
</script>
